var mysql = require('mysql');
var config = require('../config');

var connection;
function connect() {
  connection = mysql.createConnection(config.mysql)
  connection.connect(function (err) {
    if (err) {
      // 如果是连接断开，自动重新连接
      if (err.code === 'PROTOCOL_CONNECTION_LOST') {
        connect();
      } else {
        console.error(err.stack || err);
      }
      return;
    }
    console.log('connected as id ' + connection.threadId);
  });
}
connect();

module.exports = connection;